package com.xia.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xia.pojo.Book;
import com.xia.pojo.Category;
import com.xia.service.BookService;
import com.xia.service.CategoryService;
import com.xia.utils.AjaxUtils;
import com.xia.utils.Const;
import com.xia.utils.PageBean;
import com.xia.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    public BookService bookService;

    @Autowired
    public CategoryService categoryService;

    @RequestMapping("/bookPage")
    @ResponseBody
    public String bookQueryByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "limit", defaultValue = "5") Integer pageSize,
                                  String bookName, String author, String cid, HttpSession session) {

        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        // 判断搜索条件是否为空
        if (StringUtil.isNotEmpty(bookName)) {
            map.put("bookName", bookName.trim());
        }
        if (StringUtil.isNotEmpty(author)) {
            map.put("author", author.trim());
        }
        if (StringUtil.isNotEmpty(cid)) {
            map.put("cid", cid);
        }

        PageBean<Book> bookPageBean = bookService.queryByPage(map);

        List<Category> categoryList = categoryService.listCategory();
        session.setAttribute(Const.CATEGORY, categoryList);

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", bookPageBean.getTotalSize());
        result.put("data", bookPageBean.getData());

        try {
            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @RequestMapping("/queryById")
    public String bookQueryById (Integer bookId, Model model){

        Book book = bookService.queryById(bookId);
        model.addAttribute("bookInfo", book);
        return "book/addBook";
    }

    // 携带数据跳转到edit页面
    @RequestMapping("/edit")
    public String updateBookInfo (Integer bookId, Model model){
        Book book = bookService.queryById(bookId);
        model.addAttribute("bookInfo", book);
        model.addAttribute("code", 1);
        return "book/addBook";
    }

    @PostMapping("/editBook")
    @ResponseBody
    public String editBook (Book book){

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            bookService.updateBookInfo(book);
            ajaxUtils.setMessage("更新成功！");
            ajaxUtils.setCode(0);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setStatus("0");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setMessage("更新失败！");
            ajaxUtils.setCode(-1);
            ajaxUtils.setSuccess(false);
            ajaxUtils.setStatus("-1");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @PostMapping("/delBook")
    @ResponseBody
    public String delBookById (Integer bookId){

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            bookService.delBookById(bookId);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
            ajaxUtils.setMessage("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
            ajaxUtils.setMessage("删除失败！");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @RequestMapping("/toAddBook")
    public String toAddBook (){
        return "book/addBook";
    }

    @PostMapping("/addBook")
    @ResponseBody
    public String addBook (Book book){

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            bookService.addBook(book);
            ajaxUtils.setMessage("添加成功！");
            ajaxUtils.setSuccess(true);
            ajaxUtils.setStatus("0");
            ajaxUtils.setCode(0);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setMessage("添加异常！");
            ajaxUtils.setSuccess(false);
            ajaxUtils.setStatus("-1");
            ajaxUtils.setCode(-1);
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @RequestMapping("/toType")
    public String toBookType (){
        return "book/bookType";
    }
}